﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System;
using System.Runtime.InteropServices;
using Beyond;
using Beyond.Ant;

public class DUHelper {

    public static Rect getGameObjectWorldRect(GameObject obj) {
        RectTransform rt = obj.GetComponent<RectTransform>();
        //BELog.debug("DUHelper.getGameObjectWorldRect " + rt.position);
        //BELog.debug("DUHelper.getGameObjectWorldRect " + obj.transform.parent.TransformPoint(rt.localPosition));
        
        Vector3[] pos = new Vector3[4];
        rt.GetWorldCorners(pos);
        //BELog.debug("DUHelper.getGameObjectWorldRect " + pos[0] + " " + pos[2]);
        Rect rect = new Rect(pos[0].x, pos[0].y, pos[2].x - pos[0].x, pos[2].y - pos[0].y);

        //Vector3 pMin = obj.transform.parent.TransformPoint(new Vector3(rt.localPosition.x + rt.rect.xMin, rt.localPosition.y + rt.rect.yMin, rt.localPosition.z));
        //Vector3 pMax = obj.transform.parent.TransformPoint(new Vector3(rt.localPosition.x + rt.rect.xMax, rt.localPosition.y + rt.rect.yMax, rt.localPosition.z));
        //BELog.debug("DUHelper.getGameObjectWorldRect " + pMin + " " + pMax);
        //rect = new Rect(pMin.x, pMin.y, pMax.x - pMin.x, pMax.y - pMin.y);

        //BELog.debug("DUHelper.getGameObjectWorldRect " + rect);
        //BELog.debug("DUHelper.getGameObjectWorldRect " + Screen.width + ", " + Screen.height);
        return rect;
    }

    public static Rect getGameObjectWorldRectInNDC(GameObject obj) {
        Rect src = getGameObjectWorldRect(obj);
        Rect dest = new Rect(src.x / Screen.width, src.y / Screen.height, src.width / Screen.width, src.height / Screen.height);
        //BELog.debug("DUHelper.getGameObjectWorldRectInNDC " + dest);
        return dest;
    }

    public static String formatNativeAdRect(GameObject obj) {
        if (obj == null) {
            BELog.error("DUHelper.formatNativeAdRect error: obj is null");
            return "";
        }
        Rect rect = getGameObjectWorldRectInNDC(obj);
        rect.x += rect.width / 2;
        rect.y += rect.height / 2;
        rect.y = 1 - rect.y;
        String s = "x=" + rect.x + ";y=" + rect.y + ";w=" + rect.width + ";h=" + rect.height;
        //BELog.debug("DUHelper.formatNativeAdRect " + s);
        return s;
    }

}